package com.hzya.frame.hr.organization.service.impl;

import com.hzya.frame.hr.company.dao.ICompanyDao;
import com.hzya.frame.hr.company.entity.CompanyEntity;
import com.hzya.frame.hr.organization.dao.IOrganizationDao;
import com.hzya.frame.hr.organization.entity.OrganizationEntity;
import com.hzya.frame.hr.organization.entity.OrganizationNodeEntity;
import com.hzya.frame.hr.organization.service.IOrganizationService;
import com.hzya.frame.system.user.entity.UserEntity;
import com.hzya.frame.utils.core.action.SessionContext;
import com.hzya.frame.utils.core.service.BaseService;
import com.hzya.frame.utils.core.tree.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @description: 组织机构管理  service
 * @tableName: hr_organization
 * @entityName: OrganizationEntity
 * @author: gjh
 * @history: 1.0
 */
@Service(value = "hr_organizationService")
public class OrganizationServiceImpl extends BaseService<OrganizationEntity, Long> implements IOrganizationService {

    protected IOrganizationDao organizationDao;
    /**
     * 企业service
     */
    @Autowired
    private ICompanyDao companyDao;

    @Autowired
    public void setOrganizationDao(IOrganizationDao dao) {

        this.organizationDao = dao;
        this.dao = dao;
    }

    /***
     * 根据当前登陆人获取所在的公司的组织机构
     * @return
     */
    @Override
    public List<TreeNode> listQueryCompanyOrganizationTreeNode() {
        List<TreeNode> treeNodeList = new ArrayList<>();
        /** 获取当前登陆人*/
        UserEntity userEntity = SessionContext.getCurrentUser();

        /** 根据当前登陆人获取所在的企业，如果是超级管理员，获取所有的企业*/
        CompanyEntity companyEntity = new CompanyEntity();

        /** 设置当前登陆人的企业id，查询*/
        companyEntity.setUp_id(userEntity.getCompany_org_id());

        List<CompanyEntity> companyEntityList = companyDao.query(companyEntity);

        /** 循环所有的企业，查询下面的子公司，和组织机构*/
        if (companyEntityList.size() > 0) {

            companyEntityList.forEach((company) -> {

               TreeNode node = getOrganizationTreeNode(company);
                if(node != null){
                    treeNodeList.add(node);
                }
            });
        }

        return treeNodeList;
    }

    @Override
    public List<TreeNode> listCreateSubOrganizationTree(OrganizationEntity entity) {

        List<OrganizationEntity> tempList = organizationDao.query(entity);

        List<TreeNode> childNode = new ArrayList<TreeNode>();
        for (OrganizationEntity tempDomain : tempList) {
            TreeNode tempNode = new TreeNode();
            tempNode.setId(tempDomain.getId());
            tempNode.setText(tempDomain.getOrg_name());
            tempNode.setAttributes(tempDomain);
            tempNode.setParentId(tempDomain.getUp_org_id());
            tempNode.setState("open");
            childNode.add(tempNode);
        }
        return childNode;
    }

    /***
     * 查询组织机构树
     * @param companyEntity
     * @return
     */
    private TreeNode  getOrganizationTreeNode(CompanyEntity companyEntity){
        AtomicReference<TreeNode> rootNode = new AtomicReference<>();
        /** 根据企业查找企业下的所有组织机构*/
        OrganizationEntity organizationEntity = new OrganizationEntity();
        organizationEntity.setUp_org_id(-1L);
        organizationEntity.setOrg_type("company");
        organizationEntity.setCompany_id(companyEntity.getId());

        /** 查询组织机构*/
        List<OrganizationEntity> organizationEntitylist = organizationDao.query(organizationEntity);
        if(organizationEntitylist.size()>0){
            organizationEntitylist.forEach(entity -> {
                rootNode.set(getOrganizationNode(entity));
            });
         }
         return rootNode.get();
    }

    /***
     * 递归查询子节点，跳出条件：没有子节点
     * @param entity
     * @return
     */
    private TreeNode getOrganizationNode(OrganizationEntity entity) {
        TreeNode childrenNode = new TreeNode();
        /** 上级id，如果是根取id*/
         /** 查询组织机构下面是否有子节点*/
        OrganizationEntity organizationEntity = new OrganizationEntity();
        organizationEntity.setUp_org_id(entity.getId());
        List<OrganizationEntity> organizationEntityList = organizationDao.query(organizationEntity);
        if (organizationEntityList.size() > 0) {
            organizationEntityList.forEach(org -> {
                TreeNode grandsonNode = getOrganizationNode(org);
                childrenNode.setId(entity.getId());
                childrenNode.setText(entity.getOrg_name());
                childrenNode.setAttributes(new OrganizationNodeEntity(entity.getId(),entity.getUp_org_id() ,entity.getCompany_id()));
                childrenNode.setState("open");
                childrenNode.setParentId(entity.getUp_org_id());
                childrenNode.getChildren().add(grandsonNode);
            });
        } else {
            childrenNode.setId(entity.getId());
            childrenNode.setText(entity.getOrg_name());
            childrenNode.setState("open");
            childrenNode.setParentId(entity.getUp_org_id());
            childrenNode.setAttributes(new OrganizationNodeEntity(entity.getId(),entity.getUp_org_id(),entity.getCompany_id()));
        }
        return childrenNode;
    }

}
